package com.myapp.mobilesafe.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import com.myapp.mobilesafe.service.AddressService;
import com.myapp.mobilesafe.service.BlackListService;
import com.myapp.mobilesafe.service.WatchDogService;
import com.myapp.mobilesafe.utils.PrefUtils;
import com.myapp.mobilesafe.utils.ServiceStatusUtils;
import com.myapp.mobilesafe.view.SettingItemClickView;
import com.myapp.mobilesafe.view.SettingItemView;

public class SettingActivity extends AppCompatActivity {
    private SettingItemView sivUpdate;
    private SettingItemView sivAddress;
    private SettingItemView sivBlackList;
    private SettingItemView sivAppLock;
    private SettingItemClickView sicStyle;
    private SettingItemClickView sicLocation;

    private String[] mItems = new String[] { "纯净蓝", "活力橙", "金属灰", "苹果绿" };
   // private SharedPreferences sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
       // sp = getSharedPreferences("config",MODE_PRIVATE);
        initUpdate();
        initAddress();
        initAddressStyle();
        initAddressLocation();
        initBlackList();
        initAppLock();
    }

    private void initAppLock() {
        sivAppLock = (SettingItemView) findViewById(R.id.siv_app_lock);
        boolean serviceRunning = ServiceStatusUtils.isServiceRunning(
                "com.myapp.mobilesafe.service.WatchDogService", this);
        sivAppLock.setChecked(serviceRunning);

        sivAppLock.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent service = new Intent(getApplicationContext(),
                        WatchDogService.class);
                if (sivAppLock.isChecked()) {
                    sivAppLock.setChecked(false);
                    stopService(service);// 关闭看门狗的服务
                } else {
                    sivAppLock.setChecked(true);
                    startService(service);// 开启看门狗的服务
                }
            }
        });
    }

    private void initBlackList() {
        sivBlackList = (SettingItemView) findViewById(R.id.siv_blacklist);
        boolean serviceRunning = ServiceStatusUtils.isServiceRunning(
                "com.myapp.mobilesafe.service.BlackListService", this);
        sivBlackList.setChecked(serviceRunning);

        sivBlackList.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent service = new Intent(getApplicationContext(),
                        BlackListService.class);
                if (sivBlackList.isChecked()) {
                    sivBlackList.setChecked(false);
                    stopService(service);// 关闭归属地显示的服务
                } else {
                    sivBlackList.setChecked(true);
                    startService(service);// 开启归属地显示的服务
                }
            }
        });
    }

    private void initAddressLocation() {
        sicLocation = (SettingItemClickView) findViewById(R.id.sic_location);
        sicLocation.setTitle("归属地提示框位置");
        sicLocation.setDesc("设置归属地提示框的显示位置");
        sicLocation.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // 跳到位置修改的页面
                startActivity(new Intent(getApplicationContext(),
                        DragViewActivity.class));
            }
        });
    }
    private void initAddress(){
        sivAddress = (SettingItemView) findViewById(R.id.siv_address);
        boolean serviceRunning = ServiceStatusUtils.isServiceRunning("com.myapp.mobilesafe.service.AddressService", this);
        sivAddress.setChecked(serviceRunning);
        sivAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplication(), AddressService.class);
                if (sivAddress.isChecked()) {
                    sivAddress.setChecked(false);
                    stopService(intent);// 关闭归属地显示的服务
                } else {
                    sivAddress.setChecked(true);
                    startService(intent);// 开启归属地显示的服务
                }
            }
        });
    }
    private void initUpdate() {
        sivUpdate = (SettingItemView) findViewById(R.id.siv_update);
       // sivUpdate.setTvTitle("自动更新设置");
        Boolean isUpdate = PrefUtils.getBoolean("auto_update", true,this);
        sivUpdate.setChecked(isUpdate);
//        if(isUpdate){
//            sivUpdate.setTvDesc("自动更新已开启");
//            sivUpdate.setChecked(true);
//        }else {
//            sivUpdate.setTvDesc("自动更新已关闭");
//        }
        sivUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(sivUpdate.isChecked()){
                    sivUpdate.setChecked(false);
                    //sivUpdate.setTvDesc("自动更新已关闭");
                    //sp.edit().putBoolean("auto_update",false).commit();
                    PrefUtils.putBoolean("auto_update", false, getApplicationContext());
                }else {
                    sivUpdate.setChecked(true);
                    //sivUpdate.setTvDesc("自动更新已开启");
                    //sp.edit().putBoolean("auto_update",true).commit();
                    PrefUtils.putBoolean("auto_update", true, getApplicationContext());
                }
            }
        });
    }
    private void initAddressStyle() {
        sicStyle = (SettingItemClickView) findViewById(R.id.sic_style);
        sicStyle.setTitle("归属地提示框风格");

        // 获取已保存的样式
        int style = PrefUtils.getInt("address_style", 0, this);
        sicStyle.setDesc(mItems[style]);

        sicStyle.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                showChooseDialog();
            }
        });
    }
    protected void showChooseDialog() {
        // 获取已保存的样式
        int style = PrefUtils.getInt("address_style", 0, this);

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("归属地提示框风格");
        builder.setIcon(R.drawable.bgdialog);
        builder.setSingleChoiceItems(mItems, style,
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 保存当前风格
                        PrefUtils.putInt("address_style", which,
                                getApplicationContext());
                        dialog.dismiss();

                        sicStyle.setDesc(mItems[which]);// 更新描述
                    }

                });

        builder.setNegativeButton("取消", null);
        builder.show();
    }
}
